/*
 * Copyright 2016 Yan Zhenjie
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mingmen.canting.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mingmen.canting.R;
import com.mingmen.canting.adapter.YiShenHeAdapter;
import com.mingmen.canting.base.BaseBean;
import com.mingmen.canting.http.callback.StringCallback;
import com.mingmen.canting.httpconfig.HttpResponse;
import com.mingmen.canting.model.GetApplyBillDetail;
import com.mingmen.canting.model.LoginBean;
import com.mingmen.canting.util.GsonUtil;
import com.mingmen.canting.util.PinnedHeaderDecoration;
import com.mingmen.canting.util.SharePreferenceUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;


/**
 * <p>
 * ListView形式的侧滑菜单，支持横向和竖向的。
 * </p>
 * Created by Yan Zhenjie on 2016/7/27.
 */
public class YiShenHeActivity extends Activity {
    @BindView(R.id.img_return)
    ImageView imgReturn;

    @BindView(R.id.layout_close)
    LinearLayout layoutClose;
    private List<LoginBean.DataBean> mContactModels;
    private List<LoginBean.DataBean> mShowModels;
    private RecyclerView mRecyclerView;


    private YiShenHeAdapter mAdapter;
    String string;

    String token;
    String applyId;
    List<GetApplyBillDetail.DataBean> dataBeanList = new ArrayList<>();
    String position;
    @BindView(R.id.text_name)
    TextView text_name;
    @BindView(R.id.limitDate)
    TextView limitDate;
    @BindView(R.id.text_size)
    TextView text_size;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yishenhe);

        ButterKnife.bind(this);
        token = (String) SharePreferenceUtil.get(this, "token", "");
        applyId = getIntent().getStringExtra("applyId");
        mAdapter = new YiShenHeAdapter(this, dataBeanList);
        position = getIntent().getStringExtra("position");
        int i = Integer.valueOf(position) + 1;
        if (i < 10) {
            text_name.setText("0" + i + "订单");
        } else {
            text_name.setText("" + i + "订单");
        }


        // RecyclerView设置相关
        mRecyclerView = (RecyclerView) findViewById(R.id.main_recycler);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        final PinnedHeaderDecoration decoration = new PinnedHeaderDecoration();
        decoration.registerTypePinnedHeader(1, new PinnedHeaderDecoration.PinnedHeaderCreator() {
            @Override
            public boolean create(RecyclerView parent, int adapterPosition) {
                return true;
            }
        });
//        mRecyclerView.addItemDecoration(decoration);
        mRecyclerView.setAdapter(mAdapter);


        HttpResponse.showShopDetail(token, applyId, new StringCallback(this) {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                Log.d("SortXiuGaiFragmentSortXiuGaiFragment", s);
                Log.d("tokentoken", s);
                BaseBean baseBean = new BaseBean();
                baseBean = GsonUtil.GsonToBean(s, BaseBean.class);
                if (baseBean.getCode() == 0) {
                    GetApplyBillDetail shopListModel = new GetApplyBillDetail();
                    shopListModel = GsonUtil.GsonToBean(s, GetApplyBillDetail.class);
                    dataBeanList = shopListModel.getData();
                    if (dataBeanList.size() == 0) {
                        Toast.makeText(YiShenHeActivity.this, "该订单无数据", Toast.LENGTH_SHORT).show();
                        finish();
                    } else {
                        mAdapter.setDataList(dataBeanList);
                        limitDate.setText(dataBeanList.get(0).getLimitDate());
                        text_size.setText(dataBeanList.size() + "");
                    }

                }

            }
        });


    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mContactModels != null) {
            mContactModels.clear();
            mContactModels = null;
        }
    }

    @OnClick({R.id.img_return, R.id.layout_close})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.img_return:
            case R.id.layout_close:

                finish();

                break;

        }
    }

}